package com.yinhai.message.manage.statistical.controller;

import com.yinhai.core.app.ta3.web.controller.BaseController;
import com.yinhai.core.common.ta3.dto.TaParamDto;
import com.yinhai.message.manage.statistical.bpo.IMessageStatisticBpo;
import com.yinhai.message.manage.statistical.vo.MessageAnalysisVo;
import com.yinhai.modules.codetable.api.domain.vo.AppCodeVo;
import com.yinhai.modules.codetable.api.util.CodeTableUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author:TANQINGPING
 * @version:1.0 2017/4/18
 * package:com.yinhai.message.manage.statistical.controller
 * <p>Title: MessageStasticController.java</p>
 * <p>Description: 消息统计</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 四川久远银海软件股份有限公司</p>
 */
@RequestMapping("messageStatistic")
@Controller
public class MessageStatisticController extends BaseController {
    @Resource(name = "messageStatisticBpo")
    private IMessageStatisticBpo messageStatisticBpo;

    private final static String MORNING = " 00:00:00";
    private final static String EVENING = " 23:59:59";
    private final static String DAY_FORMAT = "yyyy-MM-dd";
    private final static String HOUR_FORMAT = "yyyy-MM-dd HH";
    private final static String SECOND_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private final static int DURING_DAYS = 10;
    private final static int DURING_MONTH = 30;

    @RequestMapping("messageStatisticController.do")
    public String execute(ModelMap modelMap) {
        //初始条件：最近十天
        List<String> manyDays = getDays(null, DURING_DAYS);
        modelMap.addAttribute("start", manyDays.get(0) + MORNING);
        modelMap.addAttribute("end", getNowHourStr());
        return "manage/statistical/messageStatistic";
    }

    @RequestMapping("statistic.do")
    public String getStatistic() {
        TaParamDto paramDto = getTaDto();
        SimpleDateFormat format = new SimpleDateFormat(DAY_FORMAT);
        try {
            if (StringUtils.isEmpty(paramDto.get("endDate"))) {
                paramDto.put("endDate", getNowHourStr());
            }
            if (StringUtils.isEmpty(paramDto.get("startDate"))) {
                List<String> days = getDays(format.parse("" + paramDto.get("endDate")), DURING_DAYS);
                paramDto.put("startDate", days.get(0) + MORNING);
            }
            List<String> days = getDuring("" + paramDto.get("startDate"), "" + paramDto.get("endDate"), format, Calendar.DAY_OF_MONTH);
            if (days.size() > DURING_MONTH) {
                days = days.subList(0, DURING_MONTH);
                paramDto.put("endDate", days.get(days.size() - 1) + EVENING);
            }
            List<MessageAnalysisVo> list = messageStatisticBpo.getStatisticData(paramDto);
            setData("result", list);
            setData("dates", days);
            setData("kssj", paramDto.get("startDate"));
            setData("jssj", paramDto.get("endDate"));
        } catch (ParseException e) {
            e.printStackTrace();
            setSuccess(false);
            setMessage("转换条件失败");
        }
        return JSON;
    }

    @RequestMapping("statisticByH.do")
    public String getStatisticByH() {
        TaParamDto paramDto = getTaDto();
        SimpleDateFormat format = new SimpleDateFormat(HOUR_FORMAT);
        try {
            List<String> hours = getDuring("" + paramDto.get("startDate"), "" + paramDto.get("endDate"), format, Calendar.HOUR_OF_DAY);
            List<MessageAnalysisVo> list = messageStatisticBpo.getStatisticDataByH(paramDto);
            setData("result", list);
            setData("dates", hours);
            setData("kssj", paramDto.get("startDate"));
            setData("jssj", paramDto.get("endDate"));
        } catch (ParseException e) {
            e.printStackTrace();
            setSuccess(false);
            setMessage("转换条件失败");
        }
        return JSON;
    }

    @RequestMapping("statisticTypes.do")
    public String statisticTypes() {
        TaParamDto paramDto = getTaDto();
        if (StringUtils.isEmpty(paramDto.get("begin")) || StringUtils.isEmpty(paramDto.get("end"))) {
            setSuccess(false);
            setMessage("参数为空");
        } else {
            List<MessageAnalysisVo> list = messageStatisticBpo.getTypesStatistic(paramDto);
            setData("result", list);

        }
        List<AppCodeVo> codeList = CodeTableUtil.getCodeList("SENDMODE", null);
        List<String> types = new ArrayList<>();
        for (AppCodeVo vo : codeList) {
            String desc = vo.getCodeDESC();
            types.add(desc);
        }
        setData("types", types);
        return JSON;
    }

    private List<String> getDays(Date endDate, int number) {
        SimpleDateFormat fomater = new SimpleDateFormat(DAY_FORMAT);
        List<String> dates = new ArrayList<>();
        for (int i = number - 1; i >= 0; i--) {
            Calendar calendar = Calendar.getInstance();
            if (endDate != null) {
                calendar.setTime(endDate);
            }
            int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
            calendar.set(Calendar.DAY_OF_YEAR, dayOfYear - i);
            dates.add(fomater.format(calendar.getTime()));
        }
        return dates;
    }

    private List<String> getDuring(String dBegin, String dEnd, SimpleDateFormat format, int unitType) throws ParseException {
        List<String> list = new ArrayList<>();
        Date beginD = format.parse(dBegin);
        list.add(format.format(beginD));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(beginD);
        Calendar calEnd = Calendar.getInstance();
        Date endDate = format.parse(dEnd);
        calEnd.setTime(endDate);
        while (endDate.after(calBegin.getTime())) {
            calBegin.add(unitType, 1);
            list.add(format.format(calBegin.getTime()));
        }
        return list;
    }

    private String getNowHourStr() {
        SimpleDateFormat format_2 = new SimpleDateFormat(SECOND_FORMAT);
        return format_2.format(new Date().getTime());
    }
}
